#include "shstyle.h"
#include "console.h"
#include "mcurses.h"
#include "mstring.h"
#include "ansi.h"

static mx_shstyle_t shstyle = {
    .head = {
        .length = -1,
        .logo = MX_SHSTYLE_LOGG,
        .info = " ",
        .div = "●>"
    },
    .style = {
        .current = mx_null,
        .logo = ansi_sgr_fcolor4(Blue),
        .info = "",
        .div = ansi_sgr_fcolor4(Blue),
        .content = ansi_sgr_fcolor4(Magenta),
        .warn = ansi_sgr_fcolor4(Yellow),
        .error = ansi_sgr_fcolor4(Red)
    }
};

void mx_shstyle_drawhead ()
{
    console("%s%s%s%s%s%s",
            shstyle.style.logo, shstyle.head.logo,
            shstyle.style.info, shstyle.head.info,
            shstyle.style.div, shstyle.head.div);
    shstyle.style.current = shstyle.style.div;
}

void mx_shstyle_change (mx_shstyle_mode mode)
{
    mx_string_pt style;
    switch (mode)
    {
    case MX_SHSTYLE_MODE_NONE:
        style = ANSI_CSI_SGR(0);
        break;
    case MX_SHSTYLE_MODE_LOGO:
        style = shstyle.style.logo;
        break;
    case MX_SHSTYLE_MODE_INFO:
        style = shstyle.style.info;
        break;
    case MX_SHSTYLE_MODE_DIV:
        style = shstyle.style.div;
        break;
    case MX_SHSTYLE_MODE_CONTENT:
        style = shstyle.style.content;
        break;
    case MX_SHSTYLE_MODE_WARN:
        style = shstyle.style.warn;
        break;
    case MX_SHSTYLE_MODE_ERROR:
        style = shstyle.style.error;
        break;
    }
    if (shstyle.style.current != style)
    {
        shstyle.style.current = style;
        console("%s", style);
    }
}

mx_size_t mx_shstyle_headlength ()
{
    if (shstyle.head.length == -1)
        shstyle.head.length = mx_strlen(shstyle.head.logo) +
                              mx_strlen(shstyle.head.info) +
                              mx_strlen(shstyle.head.div);
     return shstyle.head.length;
}

mx_string_pt mx_shstyle_currentstyle (void)
{
    return shstyle.style.current;
}
